package controller;

import domain.SysLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import service.ISysLogService;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author ：Yan Guang
 * @date ：Created in 2020/9/26 9:14
 * @description：  完成日志的添加
 * 也就是会说   我们在AOP中切入了一个切面  然后可以将我们的代码悄悄的执行  然后来记录一个日志
 */
@Component
@Aspect
public class LogAop {
    private Date  visitTime;
    private Class clazz;
    private Method method;


    @Autowired
    private ISysLogService sysLogService;

    @Autowired
    private HttpServletRequest request;

    //前置通知
    @Before("execution(* controller.*.*(..))")
    public void doBefore(JoinPoint jp) throws NoSuchMethodException {
        visitTime = new Date();
        clazz = jp.getTarget().getClass();
        String methodName = jp.getSignature().getName();
        Object[] args = jp.getArgs();
        if (args.length==0||args==null){
            method=clazz.getMethod(methodName);
        }else {
            Class[] classArgs = new Class[args.length];
            for (int i=0;i<args.length;i++){
                classArgs[i]=args[i].getClass();//就比方说是把 int string这种参数都存放在了一个class的数组中去
            }
            method=clazz.getMethod(methodName,classArgs);
        }
    }
    
    //后置通知
    @After("execution(* controller.*.*(..))")
    public void doAfter(JoinPoint jp) throws Exception {
        //获取访问的时长
        long time=new Date().getTime()-visitTime.getTime();

        //获取URL
        String url="";
        if (clazz!=null||method!=null||clazz!=LogAop.class){
            RequestMapping clazzAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
            if (clazzAnnotation!=null){
                String[] classValue = clazzAnnotation.value();
                RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
                if (methodAnnotation!=null){
                    String[] methodValue = methodAnnotation.value();
                    url=classValue[0]+methodValue[0];
                }
            }
        }
        //获取访问的IP
        String ip = request.getRemoteAddr();

        //获取当前操作的用户(SpringSecurity来获取)
        SecurityContext context = SecurityContextHolder.getContext();//获取当前登陆的用户
        User user = (User) context.getAuthentication().getPrincipal();//通过页面获取到我们的对象
        String username = user.getUsername();//获取用户名


        SysLog sysLog=new SysLog();
        sysLog.setUsername(username);
        sysLog.setExecutionTime(time);
        sysLog.setIp(ip);
        sysLog.setUrl(url);
        sysLog.setVisitTime(visitTime);
        sysLog.setMethod("[类名]"+clazz.getName()+"[方法名]"+method.getName());

        sysLogService.save(sysLog);
    }



}
